import java.util.*;
import java.lang.*;


public class Problem {
	private ArrayList<Integer[]> stack = new ArrayList<Integer[]>();
	
	private int maxMug1 = 0;
	private int maxMug2 = 0;
	
	private int target = 0;
	private boolean solved = false;
	
	public Problem(int m1, int m2) {
		maxMug1 = m1;
		maxMug2 = m2;
	}
	
	private boolean isInStack(int m1, int m2) {
		System.out.print("	Checking: "+m1+","+m2);
		for (int i = 0; i<stack.size(); i++) {
			Integer[] crt = stack.get(i);
			if (crt[0].intValue() == m1 && crt[1].intValue() == m2) {
				System.out.println(" = is in stack");
				return true;
			}
		}
		
		System.out.println(" = not in stack");
		return false;
	}
	
	private void addToStack(int m1, int m2) {
		stack.add(new Integer[] {Integer.valueOf(m1), Integer.valueOf(m2)});
	}
	
	public void expandDF(int m1, int m2, int r) {
		
		if (m1 == target || m2 == target) {
			System.out.println("Solved!");
			solved = true;
			return;
		}
		
		if (!isInStack(m1,m2)) {
			addToStack(m1,m2);
		} else {
			return;
		}
		
		loop:
		for (int i = 0; i<6 && !solved; i++) {
			int m[] = new int[2];
			
			System.out.println(r+") "+i+": "+m1+","+m2);
			
			switch (i) {
				case 0:
					if (m1 < maxMug1) {
						m = u1(m1,m2);
					} else {
						continue loop;
					}
					break;
					
				case 1:
					if (m2 < maxMug2) {
						m = u2(m1,m2);
					} else {
						continue loop;
					}
					break;
					
				case 2:
					if (m1 > 0) {
						m = g1(m1,m2);
					} else {
						continue loop;
					}
					break;
					
				case 3:
					if (m2 > 0) {
						m = g2(m1,m2);
					} else {
						continue loop;
					}
					break;
					
				case 4:
					if (m1 > 0 && m2 < maxMug2) {
						m = t12(m1,m2);
					} else {
						continue loop;
					}
					break;
					
				case 5:
					if (m2 > 0 && m1 < maxMug1) {
						m = t21(m1,m2);
					} else {
						continue loop;
					}
					break;
			}
			
			System.out.println("Expanding ("+m[0]+", "+m[1]+", "+(r+1)+")");
			expandDF(m[0],m[1],r+1);
		}
	}
	
	public void expandBF(ArrayList<Integer[]> rowStack, int r) {
		
		ArrayList<Integer[]> newRowStack = new ArrayList<Integer[]>();
		
		for (int j = 0; j<rowStack.size() && !solved; j++) {
			Integer[] thisRow = rowStack.get(j);
			int m1 = thisRow[0].intValue();
			int m2 = thisRow[1].intValue();
			
			loop:
			for (int i = 0; i<6 && !solved; i++) {
				int m[] = new int[2];
				
				System.out.println(r+") "+i+": "+m1+","+m2);
				
				switch (i) {
					case 0:
						if (m1 < maxMug1) {
							m = u1(m1,m2);
						} else {
							continue loop;
						}
						break;
						
					case 1:
						if (m2 < maxMug2) {
							m = u2(m1,m2);
						} else {
							continue loop;
						}
						break;
						
					case 2:
						if (m1 > 0) {
							m = g1(m1,m2);
						} else {
							continue loop;
						}
						break;
						
					case 3:
						if (m2 > 0) {
							m = g2(m1,m2);
						} else {
							continue loop;
						}
						break;
						
					case 4:
						if (m1 > 0 && m2 < maxMug2) {
							m = t12(m1,m2);
						} else {
							continue loop;
						}
						break;
						
					case 5:
						if (m2 > 0 && m1 < maxMug1) {
							m = t21(m1,m2);
						} else {
							continue loop;
						}
						break;
				}
				
				if (m[0] == target || m[1] == target) {
					System.out.println("Solved! "+m[0]+","+m[1]);
					solved = true;
					return;
				}
				
				if (!isInStack(m[0],m[1])) {
					addToStack(m[0],m[1]);
					System.out.println("Adding "+m[0]+", "+m[1]+" to expand queue.");
					newRowStack.add(new Integer[]{Integer.valueOf(m[0]), Integer.valueOf(m[1])});
				}
			}
		}
		
		
		if (newRowStack.size() > 0) {
			expandBF(newRowStack,r+1);
		}
	}
	
	public void solve(int target, int type) {
		this.target = target;
		
		solved = false;
		stack = new ArrayList<Integer[]>();
		
		if (type == 0) {
			expandDF(0,0,0);
		} else {
			ArrayList<Integer[]> rowStack = new ArrayList<Integer[]>();
			rowStack.add(new Integer[]{0,0});
			expandBF(rowStack,0);
		}
	}
	
	public int[] u1(int m1, int m2) {
		if (m1 < maxMug1) {
			m1 = maxMug1;
		}
		return new int[]{m1,m2};
	}
	
	public int[] u2(int m1, int m2) {
		if (m2 < maxMug2) {
			m2 = maxMug2;
		}
		return new int[]{m1,m2};
	}
	
	public int[] g1(int m1, int m2) {
		if (m1 > 0) {
			m1 = 0;
		}
		return new int[]{m1,m2};
	}
	
	public int[] g2(int m1, int m2) {
		if (m2 > 0) {
			m2 = 0;
		}
		return new int[]{m1,m2};
	}
	
	public int[] t12(int m1, int m2) {
		if (m2 < maxMug2 && m1 > 0) {
			if (m2+m2 > maxMug2) {
				m1 -= (maxMug2-m2);
				m2 = maxMug2;
			} else {
				int tmp = m2;
				m2 += m1;
				m1 = 0;
			}
		}
		return new int[]{m1,m2};
	}
	
	public int[] t21(int m1, int m2) {
		if (m1 < maxMug1 && m2 > 0) {
			if (m1+m2 > maxMug1) {
				m2 -= (maxMug1-m1);
				m1 = maxMug1;
			} else {
				int tmp = m1;
				m1 += m2;
				m2 = 0;
			}
		}
		
		return new int[]{m1,m2};
	}
	
	public int getMaxMug1() {
		return maxMug1;
	}
	
	public int getMaxMug2() {
		return maxMug2;
	}
}
